package cn.tedu.admin.content.dao.persist.mapper.repository.impl;

import cn.tedu.admin.content.dao.persist.mapper.CategoryMapper;
import cn.tedu.admin.content.dao.persist.mapper.repository.CategoryRepository;
import cn.tedu.admin.content.pojo.param.CategoryAddParam;
import cn.tedu.admin.content.pojo.po.CategoryPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class CategoryRepositoryImpl implements CategoryRepository {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryPO> list() {
        return categoryMapper.list();
    }

    @Override
    public Integer countByName(String name) {
        log.debug("通过类别名称:{} 查询类别数量",name);
        QueryWrapper<CategoryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        Integer count = categoryMapper.selectCount(queryWrapper);
        log.debug("通过类别名称:{} 查询类别数量结果:{}",name,count);
        return count;
    }

    @Override
    public Integer addNew(CategoryAddParam categoryAddParam) {
        log.debug("保存类别入参:{}",categoryAddParam);
        CategoryPO categoryPO = new CategoryPO();
        BeanUtils.copyProperties(categoryAddParam,categoryPO);
        log.debug("保存类别入参:{},转换为PO:{}",categoryAddParam,categoryPO);
        int rows = categoryMapper.insert(categoryPO);
        log.debug("保存类别入参:{},转换为PO:{},执行结果:{}",categoryAddParam,categoryPO,rows);
        //rows 是影响行数, id return categoryPO.getId()
        return rows;
    }

    @Override
    public CategoryPO getCategoryByParentId(Long parentId) {
        return null;
    }
}
